﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ReportViewerControl2005SandBox
{
    public partial class ParameterPanel : UserControl
    {
        public ParameterPanel()
        {
            InitializeComponent();
        }


        public void Populate(Microsoft.Reporting.WinForms.LocalReport report)
        {

            var parameters = report.GetParameters();

            foreach (var report_param in parameters)
            {
                if (report_param.DataType == Microsoft.Reporting.WinForms.ParameterDataType.String)
                {

                    var p_control = new UCStringParameter();
                    p_control.Tag = report_param;
                    p_control.ParameterValue = string.Join(",", report_param.Values.ToArray());
                    p_control.ParameterName = report_param.Prompt;
                    this.flowLayoutPanel1.Controls.Add(p_control);

                }
                else if (report_param.DataType == Microsoft.Reporting.WinForms.ParameterDataType.Boolean)
                {
                    var p_control = new UCBooleanParameter();
                    p_control.Tag = report_param;
                    p_control.ParameterValue = bool.Parse(report_param.Values[0]);
                    p_control.ParameterName = report_param.Prompt;
                    this.flowLayoutPanel1.Controls.Add(p_control);
                }
                else if (report_param.DataType == Microsoft.Reporting.WinForms.ParameterDataType.Float)
                {
                    var p_control = new UCDoubleParameter();
                    p_control.Tag = report_param;
                    p_control.ParameterValue = double.Parse(report_param.Values[0]);
                    p_control.ParameterName = report_param.Prompt;
                    this.flowLayoutPanel1.Controls.Add(p_control);
                }
                else if (report_param.DataType == Microsoft.Reporting.WinForms.ParameterDataType.DateTime)
                {
                    var p_control = new UCDateTime();
                    p_control.Tag = report_param;
                    p_control.ParameterValue = System.DateTime.Parse(report_param.Values[0]);
                    p_control.ParameterName = report_param.Prompt;
                    this.flowLayoutPanel1.Controls.Add(p_control);
                }

                else
                {
                    var p_control = new Label();
                    p_control.Text = "Unsupported parameter type for " + report_param.Name;
                    p_control.Width = 300;
                    this.flowLayoutPanel1.Controls.Add(p_control);

                }
            }

        }

        public List<Microsoft.Reporting.WinForms.ReportParameter> GetParameters(Microsoft.Reporting.WinForms.LocalReport report)
        {

            var param_controls = new List<UserControl>();
            foreach (var control in this.flowLayoutPanel1.Controls)
            {
                if ( (control is UCStringParameter)
                    || (control is UCBooleanParameter)
                    || (control is UCDoubleParameter))
                {
                    param_controls.Add( (UserControl) control);
                }
            }

            var param_objects = param_controls.Select( p => p.Tag ).Cast<Microsoft.Reporting.WinForms.ReportParameterInfo>().ToList();

            var parameters = new List<Microsoft.Reporting.WinForms.ReportParameter>();

            for (int i = 0; i < param_controls.Count; i++)
            {
                var pc = param_controls[i];
                var po = param_objects[i];

                if (po.DataType == Microsoft.Reporting.WinForms.ParameterDataType.String)
                {
                    UCStringParameter p = (UCStringParameter )pc;
                    var param = new Microsoft.Reporting.WinForms.ReportParameter(po.Name, p.ParameterValue);
                    parameters.Add(param);
                }
                else if (po.DataType == Microsoft.Reporting.WinForms.ParameterDataType.Boolean)
                {
                    UCBooleanParameter p = (UCBooleanParameter)pc;
                    var param = new Microsoft.Reporting.WinForms.ReportParameter(po.Name, p.ParameterValue.ToString() );
                    parameters.Add(param);
                }

            }

            return parameters;

        }

        private void ParameterPanel_Paint(object sender, PaintEventArgs e)
        {
            var gfx = e.Graphics;
            using(var b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(this.Width, 0),
                                                                     Color.White, Color.Gainsboro))
            {
                gfx.FillRectangle(b,this.ClientRectangle);                
            }
        }
    }

}
